package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
	. "xm_shop/database"
	. "xm_shop/models"
)

type AddressController struct{}

// 增加收货地址
func (a AddressController) AddAddress(context *gin.Context) {
	// 1. 获取用户数据
	var user User
	Cookie.Get(context, "userInfo", &user)
	// 2. 获取表单提交的数据
	phone := context.PostForm("phone")
	name := context.PostForm("name")
	address := context.PostForm("address")
	// 3. 判断收货地址的数量
	var addressNum int64
	DB.Table("address").Where("uid = ?", user.ID).Count(&addressNum)
	if addressNum > 10 {
		context.String(http.StatusBadRequest, "收货地址数量达到上限")
		return
	}
	// 4. 更新地址的默认状态
	DB.Table("address").Where("uid = ?", user.ID).Updates(map[string]interface{}{
		"default_address": 0,
	})
	// 5. 增加当前收货地址,并且设置为默认地址
	addressResult := Address{
		Uid:            user.ID,
		Phone:          phone,
		Name:           name,
		Address:        address,
		DefaultAddress: 1,
		AddTime:        int(time.Now().Unix()),
	}
	if err1 := DB.Create(&addressResult).Error; err1 != nil {
		context.String(http.StatusBadRequest, "收货地址新增失败")
		return
	}
	// 6. 返回所有地址
	var addressList []Address
	DB.Where("uid = ?", user.ID).Order("id desc").Find(&addressList)
	context.JSON(http.StatusOK, gin.H{
		"success":     true,
		"addressList": addressList,
	})
}

// 编辑收货地址
func (a AddressController) EditAddress(context *gin.Context) {
	// 获取用户信息
	var user User
	Cookie.Get(context, "userInfo", &user)
	id, err1 := strconv.Atoi(context.Param("id"))
	phone := context.PostForm("phone")
	name := context.PostForm("name")
	address := context.PostForm("address")
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	// 修改当前所有地址默认字段为0
	DB.Table("address").Where("uid = ?", user.ID).Updates(map[string]interface{}{
		"default_address": 0,
	})
	var addressObj Address
	DB.Where("id = ?", id).Take(&addressObj)
	addressObj.Name = name
	addressObj.Phone = phone
	addressObj.Address = address
	addressObj.DefaultAddress = 1
	if err2 := DB.Save(&addressObj).Error; err2 != nil {
		context.String(http.StatusBadRequest, "修改失败")
		return
	}
	// 返回用户所所有收货地址
	var addressList []Address
	DB.Where("uid = ?", user.ID).Order("id desc").Find(&addressList)
	context.JSON(http.StatusOK, gin.H{
		"success":     true,
		"addressList": addressList,
	})
}

// 获取指定收货地址
func (a AddressController) GetOneAddress(context *gin.Context) {
	id, err1 := strconv.Atoi(context.Param("id"))
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	// 获取用户信息
	var user User
	Cookie.Get(context, "userInfo", &user)
	var address Address
	DB.Where("id = ? AND uid = ?", id, user.ID).Take(&address)
	context.JSON(http.StatusOK, gin.H{
		"success": true,
		"address": address,
	})
}

// 删除收货地址
func (a AddressController) DElAddress(context *gin.Context) {
	id, err1 := strconv.Atoi(context.Param("id"))
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	var address Address
	DB.Where("id = ?", id).Take(&address)
	if err2 := DB.Delete(&address).Error; err2 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	context.String(http.StatusOK, "删除成功")
}

// 修改默认收货地址
func (a AddressController) ChangeDefaultAddress(context *gin.Context) {
	id, err1 := strconv.Atoi(context.Param("id"))
	if err1 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	// 获取用户
	var user User
	Cookie.Get(context, "userInfo", &user)
	// 修改全部收货地址的默认状态
	DB.Table("address").Where("uid = ?", user.ID).Updates(map[string]interface{}{
		"default_address": 0,
	})
	// 检索收货地址
	var address Address
	DB.Where("id = ? AND uid = ?", id, user.ID).Take(&address)
	address.DefaultAddress = 1
	if err2 := DB.Save(&address).Error; err2 != nil {
		context.String(http.StatusBadRequest, "参数有误")
		return
	}
	context.String(http.StatusOK, "修改成功")
}
